<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class login extends CI_Controller {
	var $data = array();

	public function __construct() {
		parent::__construct();

		$this -> output -> cache(0);
		$this -> data['current_module'] = site_url("login");
		$this -> data['css_url'] = base_url() . 'css/';
		$this -> data['javscript_url'] = base_url() . 'script/';
		$this -> load -> model('color');
	}

	public function index() {
		
		if (isset($_SESSION['authen']) &&$_SESSION['authen']==true) {
			
			redirect('dashboard', 'location', 301);
			
		} else {
			$this -> load -> view('login', $this -> data);
		}
	}

	public function authen() {
		$sql = "SELECT `UUID`,`UID`,`Username`,users.`Name` as FirstName,`Lastname`,role.name as rolename,users.`roleid` FROM `users` inner join role on(users.`roleid` =role.`roleid`) where username=? and password=?";
		$query = $this -> db -> query($sql, array($_REQUEST['username'], $_REQUEST['password']));
		if ($query -> num_rows()) {
			$row = $query -> row();
			$_SESSION['role_id']=$row->roleid;
			$_SESSION['FirstName']=$row->FirstName;
			$_SESSION['Lastname']=$row->Lastname;
			$_SESSION['rolename']=$row->rolename;
			
			$_SESSION['authen'] = true;
			redirect('dashboard', 'location', 301);
		} else {
			$_SESSION['authen'] = false;
			echo "<script>alert('กรูณาตรวจสอบ User password');</script>";
		}
		
	}

}
?>